package com.alcatellucent.ldap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alcatellucent.R;
import com.alcatellucent.app.LDAPApp;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;

public class MainActivity extends Activity {

	public static final String tag = "LDAP";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void onClick(View view) {

		/*
		 * switch(view.getId()) {
		 * 
		 * }
		 */
		// LDAPConnection connection = new LDAPConnection()
		String host = "172.21.190.67";
		int port = 389;
		String bindDN = "cn=directory manager";
		String bindPassword = "superuser67";
		LDAPConnection connection;

		EditText et1 = (EditText) findViewById(R.id.editText1);
		EditText et2 = (EditText) findViewById(R.id.editText2);
		EditText et3 = (EditText) findViewById(R.id.editText3);
		EditText et4 = (EditText) findViewById(R.id.editText4);

		bindDN = et1.getText().toString();
		bindPassword = et2.getText().toString();
		host = et3.getText().toString();
		port = Integer.parseInt(et4.getText().toString());

		try {

			connection = new LDAPConnection(host, port, bindDN, bindPassword);
			Log.d(tag, "Connection Successful");
			Toast.makeText(this, "Connection Successful", Toast.LENGTH_LONG)
					.show();
			((LDAPApp) getApplicationContext()).setConnection(connection);
			Intent intent = new Intent(this, HomeScreenActivity.class);
			startActivity(intent);

		} catch (LDAPException e1) {
			Log.e("LDAP", "Error connecting LDAP");
			Toast.makeText(this, "Connection Failed", Toast.LENGTH_LONG).show();
			e1.printStackTrace();
		}

	}
}
